﻿#coding:utf-8
#ultima versão funcional sem condicao de armijo

import pygame
from pygame.locals import *

import Properties
from Primitives import *
from Entities import *
from Units import Snake

class EventManager(Primitives.EventManager):

	def fireEvent(self, scene, event ):
		if event.type == QUIT:
			#pass
			scene.Snake.print_debug()
		elif event.type == KEYDOWN and event.key == K_RETURN:		
			scene.Snake._is_visible = True
			scene.Curve._is_visible = False
			
			scene.Snake.start()				

		elif event.type == KEYDOWN and event.key == K_SPACE:
			
			if scene.Snake.is_looping():		
				scene.Snake.stop()
			else:
				scene.Snake.restart()
			
		elif event.type == MOUSEBUTTONDOWN:
			scene.Snake._is_visible = False
			scene.Curve._is_visible = True
			
			scene.Curve.go_draw();
			
		elif event.type == MOUSEBUTTONUP:
			scene.Curve.stop_draw()	
			scene.Snake.set_curve( scene.Curve )
			print scene.Curve.unit.print_points()
			
		elif  event.type == KEYDOWN and event.key == K_BACKSPACE:
			scene.Snake.go_back(-1)	

		elif  event.type == KEYDOWN and event.key == K_a:				
			scene.Snake.set_manual_alfa()		
									
		elif  event.type == KEYDOWN and event.key == K_b:				
			scene.Snake.set_manual_beta()			
				
		elif  event.type == KEYDOWN and event.key == K_g:				
			scene.Snake.set_manual_gama()												
													
		elif  event.type == KEYDOWN and event.key == K_d:				
			scene.Snake.set_manual_delta()		
			
		elif  event.type == KEYDOWN and event.key == K_p:				
			scene.Snake._draw_point = not scene.Snake._draw_point
			scene.Snake._update_flag=True
			
		elif event.type == KEYDOWN and event.key == K_l:
			scene.Snake._show_dir_pt_linear = not scene.Snake._show_dir_pt_linear
			scene.Snake._update_flag=True
			
		elif event.type == KEYDOWN and event.key == K_m:
			scene.Snake.toogle_mode()	
																			
		elif  event.type == KEYDOWN:
			scene.Snake.input( event.unicode )

class InitialScene(Scene):
	
	def __init__(self):
		backSurface = pygame.Surface( (600,400) )
		backSurface = backSurface.convert()
		backSurface.fill((255, 255, 255))
		
		objects = []
		
		self._create_scene(objects)	
	
		super(InitialScene,self).__init__( EventManager(),backSurface,objects)
		
	def _create_scene(self,objects):
		textRect = pygame.Rect( (20,20), (200,30) )
		#textRect.centerx = backSurface.get_rect().centerx
		textColor = (0,0,0)
		font = pygame.font.Font(None,24)
		text = font.render( "Desenhe uma curva inicial", 1, textColor )
		
		textSpr = GameObject("Title")
		textSpr.image = text
		textSpr.rect = textRect
		
		curve = CurveEntitie("Curve")
		#curva.rect.centerx = backSurface.get_rect().centerx
		curve.rect.y = 60
	
		snake = SnakeEntitie("Snake")
		snake.set_curve( curve )
		snake._is_visible = False	
		
		objects.append( textSpr )
		objects.append( curve )
		objects.append( snake )	
		
	def loop_routine(self):
		if self.Snake.is_started():
			self.Snake.minimize()
			
